using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;
using Common.Utilities;

namespace WindowsUserControl.Dialogs
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="cols"></param>
    public delegate void TableSchemaDefined(List<TableColumn> cols);

    /// <summary>
    /// 
    /// </summary>
    public partial class TableSchemaDialog : Form
    {
        /// <summary></summary>
        public event TableSchemaDefined OnTableSchemaDefined;
        private List<TableColumn> _Cols;

        /// <summary>
        /// 
        /// </summary>
        public TableSchemaDialog()
        {
            InitializeComponent();
        }

        private void TableSchemaDialog_Load(object sender, EventArgs e)
        {
            this.PopulateDbTypes();
        }

        private void PopulateDbTypes()
        {
            this.cbo_DbType.Items.Clear();
            for (int i = 0; i <= 25; i++)
            {
                SqlDbType dbType = (SqlDbType) i;
                cbo_DbType.Items.Add(dbType.ToString());
            }
        }

        private void PopulateTableColumns()
        {
            this.dv_Schema.Rows.Clear();
            if(this._Cols !=null && this._Cols.Count>0)
            {
                this._Cols.Sort();
                foreach(TableColumn col in this._Cols)
                {
                    DataGridViewRow dr=new DataGridViewRow();
                    DataGridViewTextBoxCell cellColName=new DataGridViewTextBoxCell();
                    cellColName.Value = col.ColumnName;
                    dr.Cells.Add(cellColName);

                    DataGridViewTextBoxCell cellOrdinal=new DataGridViewTextBoxCell();
                    cellOrdinal.Value = col.Ordinal;
                    dr.Cells.Add(cellOrdinal);

                    DataGridViewComboBoxCell cellDbType=new DataGridViewComboBoxCell();
                    for(int i=0;i<=25;i++)
                    {
                        SqlDbType dbType = (SqlDbType) i;
                        cellDbType.Items.Add(dbType.ToString());
                    }
                    cellDbType.Value = col.DbType.ToString();
                    dr.Cells.Add(cellDbType);

                    DataGridViewCheckBoxCell cellNull=new DataGridViewCheckBoxCell();
                    cellNull.Value = col.AllowNull;
                    dr.Cells.Add(cellNull);

                    DataGridViewCheckBoxCell cellPK=new DataGridViewCheckBoxCell();
                    cellPK.Value = col.IsPK;
                    dr.Cells.Add(cellPK);

                    DataGridViewCheckBoxCell cellIdentity=new DataGridViewCheckBoxCell();
                    cellIdentity.Value = col.IsIdentity;
                    dr.Cells.Add(cellIdentity);

                    this.dv_Schema.Rows.Add(dr);
                }
            }
            this.dv_Schema.Refresh();
        }

        private void btn_AddCol_Click(object sender, EventArgs e)
        {
            if(this._Cols==null)
                this._Cols=new List<TableColumn>();

            string colName = this.txt_ColName.Text;
            if (string.IsNullOrEmpty(colName))
                return;
            if(this._Cols !=null && this._Cols.Count>0)
            {
                foreach(TableColumn col in this._Cols)
                {
                    if (col.ColumnName.ToLower() == colName.ToLower())
                        return;
                }
            }

            TableColumn newCol=new TableColumn();
            newCol.AllowNull = this.ck_AllowNull.Checked;
            newCol.ColumnName = colName;
            newCol.DbType = (SqlDbType) this.cbo_DbType.SelectedIndex;
            newCol.IsFK = false;
            newCol.IsIdentity = this.ck_Identity.Checked;
            newCol.IsIndexed = false;
            newCol.Ordinal = this._Cols.Count;
            if(FieldDataType.IsStringType(newCol.DbType))
            {
                newCol.Size = 255;
            }
            
            this._Cols.Add(newCol);

            this.PopulateTableColumns();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (this.OnTableSchemaDefined != null)
            {
                this.OnTableSchemaDefined(this._Cols);
            }

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}